﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form parent = null;
        int loginname = 0;
        string name = "";
        string nowdate="";
        dbutils db = new dbutils();
        public void setValue(int loginname, string name, Form f1)
        {
            this.loginname = loginname;
            this.name = name;
            parent = f1;
        }
        

        public Form2()
        {
            InitializeComponent();
        }


     

        private void label1_Click(object sender, EventArgs e)
        {
            
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            label2.Text = "你好，" + name;
            
            nowdate = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
            string sql = "select * from kq_table where date='" + nowdate + "' and staffid ='"+loginname+"'";
            if (db.select(sql) > 0)
            {
                button1.Enabled = false;
                button1.Text = "今日已打卡";
            }
            loadd1();
            loadd2();
            this.Text = "您好！" + name;


            
        }

        private void loadd1()
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from qj_table where staffid='" + loginname+"' order by qjdate DESC";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            bindingSource1.DataSource = myds.Tables["Bill"];
            dataGridView1.DataSource = bindingSource1;
            dataGridView1.Columns["num"].HeaderText = "编号";
            dataGridView1.Columns["staffid"].HeaderText = "员工编号";
            dataGridView1.Columns["staffname"].HeaderText = "员工姓名";
            dataGridView1.Columns["leavedate"].HeaderText = "请假日期";
            dataGridView1.Columns["qjdate"].HeaderText = "申请日期";
            conn.Close();
        }

        private void loadd2()
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from qj_tg_table where staffid='" + loginname+"' order by qj_tg_date DESC";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            bindingSource2.DataSource = myds.Tables["Bill"];
            dataGridView2.DataSource = bindingSource2;
            dataGridView2.Columns["num"].HeaderText = "编号";
            dataGridView2.Columns["staffid"].HeaderText = "员工编号";
            dataGridView2.Columns["staffname"].HeaderText = "员工姓名";
            dataGridView2.Columns["leavedate"].HeaderText = "请假日期";
            dataGridView2.Columns["qjdate"].HeaderText = "申请日期";
            dataGridView2.Columns["qj_tg_date"].HeaderText = "申请通过日期";
            conn.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            label1.Text = dt.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "insert into kq_table values('" + loginname + "','" + name + "','" + nowdate + "')";
            //String sql ="insert into kg_month_table values(

            string sql1 = "select * from kq_month_table where year ="+DateTime.Now.Year.ToString()+" and month="+DateTime.Now.Month.ToString()+" and staffid='"+loginname+"'";
            if (db.select(sql1) == 0)
            {
                string sql2 = "insert into kq_month_table values("+DateTime.Now.Year.ToString()+","+DateTime.Now.Month.ToString()+",'"+loginname+"','"+name+"',0,0,0)";
                if (db.insert(sql2) == 0)
                {
                    MessageBox.Show("error!");
                    return;
                }
            }
            string sql3 = "update kq_month_table set checkin_num=checkin_num+1,tot_num=tot_num+1 where year =" + DateTime.Now.Year.ToString() + " and month=" + DateTime.Now.Month.ToString() + " and staffid='" + loginname + "'";

            if ((db.insert(sql) > 0)&&(db.insert(sql3)>0))
            {
                MessageBox.Show("打卡成功！");
                button1.Text = "今日已打卡";
                button1.Enabled = false;
                return;
            }
            MessageBox.Show("打卡失败！");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int year = int.Parse(DateTime.Now.Year.ToString());
            int days = int.Parse(DateTime.Now.DayOfYear.ToString());

            int dtpyears = int.Parse(dateTimePicker1.Value.Year.ToString());
            int dtpdays = int.Parse(dateTimePicker1.Value.DayOfYear.ToString());

            if ((dtpyears <=  year)&&(dtpdays < days))
            {
                MessageBox.Show("你只能选择最近的7天进行请假！");
                return;
            }
            string choosedate =
                dateTimePicker1.Value.Year.ToString() + "-" + dateTimePicker1.Value.Month.ToString() + "-" + dateTimePicker1.Value.Day.ToString();
            if (MessageBox.Show("确定选择" + choosedate + "吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string sql1="select * from qj_table where staffid='"+loginname+"' and leavedate='"+choosedate+"'";
                if(db.select(sql1)>0){
                    MessageBox.Show("你已经提交了这一天的请假申请！不能重复提交");
                    return;
                }
                string sql = "insert into qj_table (staffid,staffname,leavedate,qjdate) values('"+loginname+"','"+name+"','"+choosedate+"','"+nowdate+"')";
                if (db.insert(sql) > 0)
                {
                    MessageBox.Show("请假成功！");
                    loadd1();
                    return;
                }
                MessageBox.Show("请假失败！");
            }

        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }

        

        private void button3_Click(object sender, EventArgs e)
        {
            ((Form1)parent).show1();
            this.Close();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            PX px = new PX();
            px.setValue(loginname, name);
            px.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            staffpj sp = new staffpj();
            sp.set_value(loginname.ToString(), name);
            sp.Show();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            mytask my = new mytask();
            my.set_value(loginname.ToString(), name);
            my.Show();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            myjx mj = new myjx();
            mj.set_value(loginname.ToString(), name);
            mj.Show();
        }
    }
}